package io.renren.modules.merchant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.modules.dishes.entity.MerchantEntity;
import io.renren.modules.merchant.dao.MerchantDao;
import io.renren.modules.merchant.dao.StatisticDao;
import io.renren.modules.merchant.entity.MerchantStatistic;
import io.renren.modules.merchant.service.MerchantService;
import io.renren.modules.merchant.service.StatisticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;


@Service
public class StatisticsImpl extends ServiceImpl<StatisticDao, MerchantStatistic> implements StatisticService {
    @Autowired
    private StatisticDao statisticDao;


    @Override
    public Boolean refreshToday() {
        int dayOfMonth = LocalDate.now().getDayOfMonth();
        UpdateWrapper<MerchantStatistic> wrapper = new UpdateWrapper<>();
        wrapper.eq("date", dayOfMonth);
        wrapper.set("turnover", 0)
                .set("order_quantity", 0); // 注意：假设"order_quantity"是字段名，根据实际情况调整
        int result = this.baseMapper.update(null, wrapper);
        return null;
    }

    @Override
    public MerchantStatistic getToday(String merchantId) {
        int dayOfMonth = LocalDate.now().getDayOfMonth();
        QueryWrapper<MerchantStatistic> wrapper=new QueryWrapper<MerchantStatistic>()
                .eq("merchant_id",merchantId)
                .eq("date",dayOfMonth);
        return statisticDao.selectOne(wrapper);
    }

    @Override
    public List<MerchantStatistic> getMonth(String merchantId) {
        int dayOfMonth = LocalDate.now().getDayOfMonth();
        List<Integer> list=new ArrayList<>();
        for (int i=1;i<=dayOfMonth;i++){
            list.add(i);
        }
        QueryWrapper<MerchantStatistic> wrapper=new QueryWrapper<MerchantStatistic>()
                .eq("merchant_id",merchantId)
                .in("date",list);
        return statisticDao.selectList(wrapper);
    }

    @Override
    public MerchantStatistic getMonthEntire(String merchantId) {
        int dayOfMonth = LocalDate.now().getDayOfMonth();
        List<Integer> list=new ArrayList<>();
        for (int i=1;i<=dayOfMonth;i++){
            list.add(i);
        }
        QueryWrapper<MerchantStatistic> wrapper=new QueryWrapper<MerchantStatistic>()
                .eq("merchant_id",merchantId)
                .in("date",list);
        List<MerchantStatistic> merchantStatistics = statisticDao.selectList(wrapper);
        MerchantStatistic merchantStatistic = new MerchantStatistic();
        if (merchantStatistics.size()>0){
            int turnover = 0;
            int orderQuantity = 0;
            for (MerchantStatistic merchantStatistic1 : merchantStatistics) {
                turnover+=merchantStatistic1.getTurnover();
                orderQuantity+=merchantStatistic1.getOrderQuantity();
            }
            merchantStatistic.setTurnover(turnover);
            merchantStatistic.setOrderQuantity(orderQuantity);
        }
        return merchantStatistic;
    }
}
